package com.qiall.boot.userservice.Impl.user.activity;


import com.qiall.boot.userservice.data.entity.OrganizationInfo;
import com.qiall.boot.userservice.data.entity.RoleInfo;
import com.qiall.boot.userservice.data.entity.UserInfo;
import com.qiall.boot.userservice.data.entity.UserQueryBean;
import com.qiall.boot.userservice.data.enums.SexEnum;
import com.qiall.boot.userservice.data.mapper.OrganizationInfoMapper;
import com.qiall.boot.userservice.data.mapper.RoleMapper;
import com.qiall.boot.userservice.data.mapper.UserMapper;
import com.qiall.datautil.RandomUtil;
import com.qiall.user.ability.entity.CreateUserDTO;
import com.qiall.user.ability.entity.UserInfoDTO;
import com.qiall.user.ability.entity.UserPageQueryDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 吴金辉
 */
@Component
public class UserConvert {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private OrganizationInfoMapper organizationInfoMapper;

    @Autowired
    private UserMapper userMapper;

    public UserInfo transToUserInfo(CreateUserDTO dto) {
        UserInfo userInfoDTO = new UserInfo();
        userInfoDTO.setRoleId(dto.getRoleId());
        userInfoDTO.setUserAccount(dto.getAccount());
        userInfoDTO.setUserNick(dto.getUserName());
        userInfoDTO.setUserPassword(dto.getPassword());
        userInfoDTO.setPhone(dto.getPhone());
        userInfoDTO.setOrgId(dto.getOrgId());
        userInfoDTO.setSex(SexEnum.ofDesc(dto.getSex()).getCode());
        userInfoDTO.setAge((dto.getAge()));
        userInfoDTO.setId(getUserId());
        return userInfoDTO;
    }

    public UserInfoDTO transToUserInfoDTO(UserInfo userInfo) {
        UserInfoDTO userInfoDTO = new UserInfoDTO();
        RoleInfo roleInfo = roleMapper.select(userInfo.getRoleId());
        if(userInfo.getOrgId()!=null){
            OrganizationInfo organizationInfo = organizationInfoMapper.select(userInfo.getOrgId());
            userInfoDTO.setOrgId(organizationInfo.getId());
            userInfoDTO.setOrgName(organizationInfo.getOrgName());
        }
        userInfoDTO.setUserId(userInfo.getId());
        userInfoDTO.setUserName(userInfo.getUserAccount());
        userInfoDTO.setAge(userInfo.getAge());
        userInfoDTO.setSexName(SexEnum.ofCode(userInfo.getSex()).getDesc());
        userInfoDTO.setSex(userInfo.getSex());
        userInfoDTO.setRoleId(userInfo.getRoleId());
        userInfoDTO.setRoleName(roleInfo.getRoleName());
        userInfoDTO.setPhone(userInfo.getPhone());
        userInfoDTO.setAccount(userInfo.getUserAccount());
        userInfoDTO.setCreateTime(userInfo.getCreateTime());
        userInfoDTO.setUpdateTime(userInfo.getUpdateTime());
        return userInfoDTO;
    }

   public UserQueryBean trans(UserPageQueryDTO userPageQueryDTO) {
        UserQueryBean userInfo = new UserQueryBean();

        userInfo.setRoleId(userPageQueryDTO.getQueryData().getRoleId());
        userInfo.setUserAccount(userPageQueryDTO.getQueryData().getAccount());
        userInfo.setUserNick(userPageQueryDTO.getQueryData().getUserName());
        userInfo.setUserPassword(userPageQueryDTO.getQueryData().getPassword());
        userInfo.setPhone(userPageQueryDTO.getQueryData().getPhone());
        userInfo.setOrgId(userPageQueryDTO.getQueryData().getOrgId());

        userInfo.setSex(userPageQueryDTO.getQueryData().getSex());
        userInfo.setAge(userPageQueryDTO.getQueryData().getAge());
        userInfo.setId(userPageQueryDTO.getQueryData().getUserId());
        userInfo.setCreateTime(userPageQueryDTO.getQueryData().getCreateTime());
        userInfo.setUpdateTime(userPageQueryDTO.getQueryData().getUpdateTime());
        userInfo.setStartTime(userPageQueryDTO.getPageQuery().getStartTime());
        userInfo.setEndTime(userPageQueryDTO.getPageQuery().getEndTime());
      return userInfo;
    }

    private Integer getUserId() {

        while (true) {
            Integer userId = RandomUtil.getRandomInt(Integer.MAX_VALUE);
            UserInfo userInfo = userMapper.select(userId);
            if (userInfo == null) {
                return userId;
            }
        }
    }


}
